//
//  LPQuestionnaireView.m
//  muce
//
//  Created by Pan on 2018/3/19.
//  Copyright © 2018年  yunshao. All rights reserved.
//

/** 问卷调查视图 */

#import "LPQuestionnaireView.h"
#import "pch.h"
#import "LPQuestionItemView.h"

#define TAG_questionView   23211

@interface LPQuestionnaireView ()<UIAlertViewDelegate>{
    
    NSMutableArray *answerArray;
}

@property (nonatomic, strong) NSDictionary *dictioanry;

@end

@implementation LPQuestionnaireView

- (instancetype)initWithFrame:(CGRect)frame dictioanry:(NSDictionary *)dictionary {
    
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = DSColorWithHex(0xf2f2f2);
        _dictioanry = dictionary;
        
        [self createUI];
    }
    return self;
}

- (void)createUI {
    
    CGFloat offsetY = 0;
    
    // 问卷标题
    UILabel *titleLabel = [self addLableWithMaxWidth:DSDevice_W - DSFixedPX(40) * 2 height:DSFixedPX(100) string:[NSString stringWithFormat:@"项目名称：%@", _dictioanry[@"paper_title"]] font:DSFontSize(16) textColor:DSColorWithHex(0x333333) numberOfLines:0 sizfTofit:YES];
    titleLabel.center = CGPointMake(DSDevice_W / 2.0, DSFixedPX(30) + CGRectGetHeight(titleLabel.frame) / 2.0);
    [self addSubview:titleLabel];
    
    // 问卷说明
    UILabel *explainLabel = [self addLableWithMaxWidth:DSDevice_W - DSFixedPX(40) * 2 height:999 string:[NSString stringWithFormat:@"%@", _dictioanry[@"paper_explain"]] font:DSFontSize(16) textColor:DSColorWithHex(0x333333) numberOfLines:0 sizfTofit:YES];
    explainLabel.center = CGPointMake(DSDevice_W / 2.0, CGRectGetMaxY(titleLabel.frame) + DSFixedPX(30) + CGRectGetHeight(explainLabel.frame) / 2.0);
    [self addSubview:explainLabel];
    
    offsetY = CGRectGetMaxY(explainLabel.frame);
    // 问题
    for (int i = 0; i < [_dictioanry[@"list"] count]; i ++) {
        // 题
        LPQuestionItemView *itemView = [[LPQuestionItemView alloc] initWithFrame:CGRectMake(0, offsetY + DSFixedPX(25), self.frame.size.width, 999) dictionary:_dictioanry[@"list"][i] questionIndex:i];
        offsetY = CGRectGetMaxY(itemView.frame);
        itemView.tag = TAG_questionView + i;
        [self addSubview:itemView];
        
//        // 填充上次记录答题内容
//        NSArray *answerArray = [[NSArray alloc] initWithArray:_dictioanry[@"answerArray"]];
//        if (answerArray.count == [_dictioanry[@"list"] count]) {
//            // 填充内容
//            [itemView fillContentWithLastAnswer:answerArray[i]];
//        }
    }
    
    // 提交问卷
    UIButton *submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(DSFixedPX(60), offsetY + DSFixedPX(60), self.frame.size.width - DSFixedPX(60) * 2, DSFixedPX(88))];
    [submitBtn setTitle:@"提交问卷" forState:UIControlStateNormal];
    [submitBtn setTitleColor:DSColorWithHex(0xffffff) forState:UIControlStateNormal];
    [submitBtn setBackgroundColor:DSColorWithHex(0xf6551e)];
    submitBtn.titleLabel.font = DSFontSize(18);
    submitBtn.layer.cornerRadius = DSFixedPX(88) / 2.0;
    submitBtn.layer.masksToBounds = YES;
    [submitBtn addTarget:self action:@selector(submitButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:submitBtn];
    
    offsetY = CGRectGetMaxY(submitBtn.frame);
    
    self.contentSize = CGSizeMake(self.frame.size.width, offsetY + DSFixedPX(60));
}


/** Label标签 */
-(UILabel *)addLableWithMaxWidth:(CGFloat)maxWidth
                           height:(CGFloat)height
                           string:(NSString *)string
                             font:(UIFont *)font
                        textColor:(UIColor *)textColor
                    numberOfLines:(int)numberOfLines
                        sizfTofit:(BOOL)sizfTofit
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, maxWidth, height)];
    if ([string isKindOfClass:[NSNull class]]) {
        string = @"";
    }
    if (string.length <= 0) {
        string = @"";
    }
    label.text = string;
    
    label.font = font;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = textColor;
    label.numberOfLines = numberOfLines;
    [label sizeToFit];
    if (sizfTofit) {
        if (CGRectGetWidth(label.bounds) > maxWidth) {
            label.bounds = CGRectMake(0, 0, maxWidth, CGRectGetHeight(label.bounds));
        }
    }else{
        label.bounds = CGRectMake(0, 0, maxWidth, height);
    }
    return label;
}

#pragma mark ============================= 提交点击 ==============================
- (void)submitButtonClick:(UIButton *)sender {
    
    
    //发出一个通知，在发出通知之前，判断用户是否做完了测评所有的题，如果是预览用户的话，不需要做完题，都可以发出通知，不用提交数据。
    //接受通知的地方：1
    
   
    

    answerArray = [NSMutableArray array];
    for (int i = 0; i < [_dictioanry[@"list"] count]; i ++ ) {
        LPQuestionItemView *itemView = [self viewWithTag:TAG_questionView + i];
        NSString *answerStr = [itemView.answerDictionary valueForKey:@"answer"];
        if (answerStr.length > 0) {
            // 有答案，添加到数组
            [answerArray addObject:itemView.answerDictionary];
        }
    }
    
    if (answerArray.count == [_dictioanry[@"list"] count]){
        UIAlertView *WXinstall=[[UIAlertView alloc]initWithTitle:@"" message:@"感谢测评" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];//一般在if判断中加入
        [WXinstall show];
    }else{
         [SVProgressHUD showErrorWithStatus:@"请先完善问卷调查"];
    }
    

    
  
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([[LPCommonTool readIdengit] integerValue] == 2) {
        if (self.submitBlock) {
            self.submitBlock(answerArray);
            [[NSNotificationCenter defaultCenter] postNotificationName:@"topicIsFished" object:nil];
        }
        
        //直接提交即可，不传数据过去。
    }else if ([[LPCommonTool readIdengit] integerValue] == 1){
        
        if (self.submitBlock) {
            self.submitBlock(nil);
            [[NSNotificationCenter defaultCenter] postNotificationName:@"topicIsFished" object:nil];
        }
        
      
    }
}
   

@end
